<?php
/*
	Get all social stats
*/

class social extends seo_engine
{
	public function get_social( $domain_root )
	{
		$url="http://api.sharedcount.com/?url=".rawurlencode($domain_root);

		var_dump($url);

		$options = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_SSL_VERIFYPEER, false
		);
		
		$ch = curl_init($url);
		curl_setopt_array($ch, $options);
		$content = curl_exec($ch);
		curl_close($ch);
		$counts = json_decode($content, true);
		
		$stats['social']['twitter']=$counts["Twitter"];
		$stats['social']["fb_like"]=$counts["Facebook"]["like_count"];
		$stats['social']["fb_share"]=$counts["Facebook"]["share_count"];
		$stats['social']["delicious"]=$counts["Twitter"];
		$stats['social']["stumble"]=$counts["StumbleUpon"];
		$stats['social']["reddit"]=$counts["Reddit"];
		$stats['social']["plus"]=$counts["GooglePlusOne"];
		$stats['social']["pinterest"]=$counts["Pinterest"];
		$stats['social']["linkedin"]=$counts["LinkedIn"];
		
		return $stats;
	}

	public function bulk_social( $array )
	{
		for( $int = 0; $int<strlen($array); $int++)
		{
			$stats[$int]['social'] = $this->get_social( $array[$int] );
		}

		return $stats;
	}

	public function get_template( $action = false )
	{
		// safety, prevent other cases
		if(!is_numeric($action))
			$action = 0;

		switch($action)
		{
			case 0:
				$t = "<form method='GET'>
					<input type='hidden' name='page' value='".get_class($this)."' />
					<input type='hidden' name='action' value='1' />
					<input type='text' name='url' value='http://'>
					<input type='submit' value='Submit'/>
				</form>";
			break;

			case 1:
				//global $seo;
				// make sure that url was submitted
				if(!isset($_GET['url']))
				{
					$t = "Error, no domain was received";
					break;
				}
				
				$url = $_GET['url'];
				if( !preg_match('#http#', $url) )
				{ 	
					$t = "Error, not a valid url inserted!";
					break;
				}

				// save domain root
				$stats 	= $this->get_social($url);
					
				var_dump($stats);			

				$t = "<table border=1px><tr>
					  <th>Twitter</th><th>Facebook Likes</th><th>Facebook shares</th><th>Google+</th><th>Reddit</th>
					  <th>StumbleUpon</th><th>Pinterest</th><th>LinkedIn</th></tr>
					  <tr><td>".$stats['social']['twitter']."</td> 
					  <td>".$stats['social']["fb_like"]."</td>
					  <td>".$stats['social']["fb_share"]."</td>
					  <td>".$stats['social']["plus"]."</td>
					  <td>".$stats['social']["reddit"]."</td>
					  <td>".$stats['social']["stumble"]."</td>
					  <td>".$stats['social']["pinterest"]."</td>
					  <td>".$stats['social']["linkedin"]."</td>
					 
					  </tr></table>";
			break;

			default:
				$t = "Page doesn't exist";
		}

		return $t;
	}
}
?>